/*
* frame.h -- utilities for process digital video frames
* Copyright (C) 2000 Arne Schirmacher <arne@schirmacher.de>
* Copyright (C) 2001-2007 Dan Dennedy <dan@dennedy.org>
* Copyright (C) 2007 Stéphane Brunner <stephane.brunner@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifndef _FRAME_H
#define _FRAME_H 1

#include <iostream>
using std::cerr;
using std::endl;

#include <time.h>
#include <string>
#include <stdio.h>
#include <samplerate.h>

#ifdef HAVE_LIBDV
#include <libdv/dv.h>
#include <libdv/dv_types.h>
#else
#define DV_AUDIO_MAX_SAMPLES 1944
#endif

#if defined(HAVE_LIBAVCODEC)
extern "C"
{
#include <avcodec.h>
#include <avformat.h>
#ifdef HAVE_SWSCALE
#include <swscale.h>
#endif
}
#endif

#define FRAME_MAX_WIDTH 720
#define FRAME_MAX_HEIGHT 576

typedef struct Pack
{
	/// the five bytes of a packet
	unsigned char data[ 5 ];
}
Pack;

typedef struct TimeCode
{
	int hour;
	int min;
	int sec;
	int frame;
}
TimeCode;


typedef struct AudioInfo
{
	int frames;
	int frequency;
	int samples;
	int channels;
	int quantization;
}
AudioInfo;


class VideoInfo
{
public:
	int width;
	int height;
	bool isPAL;
	TimeCode timeCode;
	struct tm	recDate;

	VideoInfo();
};


class Frame
{
public:
	unsigned char *data;
	/// the number of bytes written to the frame
	int bytesInFrame;

#if defined(HAVE_LIBAVCODEC)
	AVCodecContext *libavcodec;
#if defined(HAVE_SWSCALE)
	struct SwsContext *imgConvertRgbCtx;
	struct SwsContext *imgConvertYuvCtx;
#endif
#endif

#ifdef HAVE_LIBDV
	dv_decoder_t *decoder;
	dv_encoder_t *encoder;
#endif

public:
	Frame();
	~Frame();

	Frame& operator=( const Frame& );
	bool GetSSYBPack( int packNum, Pack &pack ) const;
	bool GetVAUXPack( int packNum, Pack &pack ) const;
	bool GetAAUXPack( int packNum, Pack &pack ) const;
	int GetFrameSize( void ) const;
	float GetFrameRate( void ) const;
	bool IsPAL( void ) const;
	bool IsNewRecording( void ) const;
	bool IsNormalSpeed( void ) const;
	bool IsComplete( void ) const;

#ifdef HAVE_LIBDV
	void ExtractHeader( void );
	void Deinterlace( AVPicture *pdst, AVPicture *psrc, int stride, int height );
	int ExtractRGB( AVPicture *rgb, bool fast );
	int ExtractPreviewRGB( AVPicture *rgb, bool fast );
	int ExtractBGR( AVPicture *rgb, bool fast );
	int ExtractPreviewBGR( AVPicture *rgb, bool fast );
	int ExtractYUV( AVPicture *yuv, bool fast );
	int ExtractYUV420( uint8_t *yuv, uint8_t *output[ 3 ] );
	int ExtractPreviewYUV( AVPicture *yuv, bool fast );
	void GetUpperField( AVPicture *image, int bpp );
	void GetLowerField( AVPicture *image, int bpp );
	bool IsWide( void ) const;
	int GetWidth();
	int GetHeight();
	bool CreateEncoder( bool isPAL, bool isWide );
	int CalculateNumberSamples( int frequency, int iteration );
	void EncodeRGB( uint8_t *rgb );
#endif

private:
	/// flag for initializing the lookup maps once at startup
	static bool maps_initialized;
	/// lookup tables for collecting the shuffled audio data
	static int palmap_ch1[ 2000 ];
	static int palmap_ch2[ 2000 ];
	static int palmap_2ch1[ 2000 ];
	static int palmap_2ch2[ 2000 ];
	static int ntscmap_ch1[ 2000 ];
	static int ntscmap_ch2[ 2000 ];
	static int ntscmap_2ch1[ 2000 ];
	static int ntscmap_2ch2[ 2000 ];
	static short compmap[ 4096 ];
};

#define BUFFER_LEN 20480

#endif
